import {
	rejects
} from "assert";
import store from "./storage.js";
import config from '@/config'

export const APIURL = config.baseUrl;

const timeout = 60000;

let tokenId = uni.getStorageSync('tokenId') || "";

// 现有的 post 函数
export const post = (url, params) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: APIURL + url,
			method: 'POST',
			data: params,
			header: {
				"userToken": tokenId
			},
			timeout,
			success: (res) => {
				if (res.data.code == 200) {
					resolve(res.data);
				} else if (res.data.code == 400) {
					uni.showToast({
						title: res.data.message || '认证失败',
						icon: "none",
						mask: true,
						duration: 1500
					});
					store.clearStorage();
				} else {
					uni.showToast({
						title: res.data.message || '请求失败',
						icon: "none",
						mask: true,
						duration: 1500
					});
					reject(new Error(`Request failed with status ${res.data.code}`));
				}
			},
			fail: () => {
				uni.showToast({
					title: 'Network error',
					icon: "none",
					mask: true,
					duration: 1500
				});
				reject(new Error('Network error'));
			}
		});
	});
};

// 现有的 postForm 函数
export const postForm = (url, params) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: APIURL + url,
			method: 'POST',
			data: params,
			header: {
				"Content-Type": "application/x-www-form-urlencoded",
				"userToken": tokenId
			},
			timeout,
			success: (res) => {
				if (res.data.code == 200) {
					resolve(res.data);
				} else if (res.data.code == 401) {
					uni.showToast({
						title: res.data.message || '认证失败',
						icon: "none",
						mask: true,
						duration: 1500
					});
					store.clearStorage();
				} else if (res.data.code == 400) {
					uni.showToast({
						title: res.data.message || '认证失败',
						icon: "none",
						mask: true,
						duration: 1500
					});
				} else {
					uni.showToast({
						title: res.data.message || '请求失败',
						icon: "none",
						mask: true,
						duration: 1500
					});
					reject(new Error(`Request failed with status ${res.data.code}`));
				}
			},
			fail: () => {
				uni.showToast({
					title: '网络错误',
					icon: "none",
					mask: true,
					duration: 1500
				});
				reject(new Error('网络错误'));
			}
		});
	});
};

// 现有的 fileUpLoad 函数
export const fileUpLoad = (url, params) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: APIURL + url,
			filePath: params,
			header: {
				"userToken": tokenId
			},
			name: 'file',
			success: (res) => {
				if (res.statusCode === 200) {
					try {
						let result = JSON.parse(res.data);
						if (result.code === 200 && result.data) {
							resolve(result.data);
						} else {
							resolve(res.data);
						}
					} catch (error) {
						resolve(res.data);
					}
				} else {
					reject(new Error(`Request failed with status ${res.statusCode}`));
				}
			},
			fail: () => {
				uni.showToast({
					title: '网络错误',
					icon: "none",
					mask: true,
					duration: 1500
				});
				reject(new Error('网络错误'));
			}
		});
	});
};